package com.admin.education.controllers.business;

import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.categorys.CategorysTransfer;
import com.admin.education.dataTransferObject.categorys.Request.CreateCategoryRequest;
import com.admin.education.dataTransferObject.categorys.Request.UpdateCategoryRequest;
import com.admin.education.services.Interfaces.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@JwtAuthorize
@RestController(value = "adminCategoryController")
@RequestMapping("categorys")
@CrossOrigin(origins = "*", maxAge = 3600)
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    @GetMapping("")
    public Callable<IResponseMessage> queryCategorys(@Valid PageRequest pageRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult<PageResponse<CategorysTransfer>> servicesExcuteResult = categoryService.queryCategorys(pageRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<CategorysTransfer>>(Result.Success,servicesExcuteResult.getData());
                }
                else{
                    throw new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return  responseMessage;
            }
        };
    }
    @PostMapping("")
    public Callable<IResponseMessage> createCategorys(@RequestBody @Valid CreateCategoryRequest createCategoryRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = categoryService.createCategory(createCategoryRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PutMapping("{categoryId}")
    public Callable<IResponseMessage> updateCategorys(@PathVariable Long categoryId,@RequestBody @Valid UpdateCategoryRequest updateCategoryRequest){
        return  new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = categoryService.updateCategory(categoryId,updateCategoryRequest);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PutMapping("{categoryId}/valid")
    public Callable<IResponseMessage> validCategory(@PathVariable Long  categoryId)
    {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult servicesExcuteResult = categoryService.setValid(categoryId);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("{id}")
    public Callable<IResponseMessage> queryCategoryById(@PathVariable Long id)
    {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                ServicesExcuteResult<CategorysTransfer> servicesExcuteResult = categoryService.queryCategoryById(id);
                if(servicesExcuteResult.isSuccess()){
                    responseMessage = new ResponseMessageWithData<CategorysTransfer>(Result.Success,servicesExcuteResult.getData());
                }
                else{
                    throw  new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

}
